import { defineStore } from "pinia";
import { pinia } from '@/store/index'
import { useWebSocketStoreWithOut } from "./websocket";
import allApi from '@/api/index'
import utils from '@/utils/index'
import { set } from "lodash-es";

export const useCommonStore = defineStore('common', {
  state: () => ({

    authId: '',//租户id
    userId: '',//用户id
    userInfo: {},//用户信息
    token: '', //权限token
    refreshToken: '', // 刷新token

    posId: '1920743017033097218',//posId
    posNo: '', // pos机号
    snNo: '', // sn号
    merchantId: '', // 门店id
    merchantName: '', // 门店名称
    appVersion: '',//app版本
    deviceInfo: {},//设备信息
    posConfig: {},//收银机配置
    secondSwiper: [],//副屏轮播图
    lockPassword: '',//锁屏密码
    memberInfo: {},//会员信息
    saomaType: '',//searchGoods销售收银商品搜索框，memberValue会员登录扫码 》》》》》》》》 当前扫码信息用作哪里  ，因为扫码监听是全局监听，需判断用在哪里
    ovlSaomaType: '',
    guadanList: [],
  }),
  getters: {
    isLogin: (state) => {
      return !!state.token
    },
    isMember: (state) => {
      return Object.keys(state.memberInfo).length > 0
    },
    getGuadanList: (state) => {
      // 只返回72小时内的挂单
      return state.guadanList.filter(item => {
        return (new Date(item.date).getTime() + 72 * 60 * 60 * 1000) > new Date().getTime()
      })
    }
  },
  actions: {
    setGuadanList (list) {
      this.guadanList = list
    },
    // 获取收银机副屏设置
    async getSecondConfig () {
      const res = await allApi.ecm.posSecondScreenConfig.getSecondImgUrls(this.merchantId)
      this.secondSwiper = res.data
      // console.log('获取收银机副屏设置', res.data)
      // utils.sendDataToSecondScreen({
      //   type: 'swiper',
      //   data: res.data[0] || {}
      // })
    },
    // 获取收银机配置
    async getPosConfig () {
      if (!this.isLogin) return
      const res = await allApi.ecm.posConfig.getByMerchantId(this.merchantId)
      this.posConfig = res.data
      // console.log('获取收银机配置', res.data)
      // utils.sendDataToSecondScreen({
      //   type: 'setting',
      //   data: res.data || {}
      // })
    },
    // 设置最新扫码类型
    setSaomaType (type) {
      this.ovlSaomaType = JSON.parse(JSON.stringify(this.saomaType))
      this.saomaType = type
    },
    // 返回上一个扫码类型
    backToSaomaType () {
      this.saomaType = JSON.parse(JSON.stringify(this.ovlSaomaType))
    },
    setLockPassword (v) {
      this.lockPassword = v
    },
    setDeviceInfo () {
      const info = uni.getSystemInfoSync()
      const snNo = utils.getSn()
      console.log('获取设备信息', info, snNo)
      this.snNo = snNo
      this.deviceInfo = info
      this.appVersion = info.appVersion
    },
    setPosId (id) {
      this.posId = id
    },
    setPosNo (posNo) {
      this.posNo = posNo
    },
    setSnNo (snNo) {
      this.snNo = snNo
    },
    setMerchantId (merchantId) {
      this.merchantId = merchantId
    },
    setMerchantName (merchantName) {
      this.merchantName = merchantName
    },
    setAuthId (authId) {
      this.authId = authId
    },
    setUserId (userId) {
      this.userId = userId
    },
    setUserInfo (userInfo) {
      this.userInfo = userInfo
    },
    // 清除所有数据
    async clearAll () {
      this.posNo = ''
      this.snNo = ''
      this.merchantId = ''
      this.merchantName = ''
      this.authId = ''
      this.userId = ''
      this.userInfo = {}

    },
    async logout () {
      // 清除锁屏密码
      this.lockPassword = ''

      // 清除用户信息
      this.userId = ''
      this.userInfo = {}
      this.token = ''
      this.refreshToken = ''

      // 接口退出登录
      await allApi.system.authLogout()
      // 断开登录状态
      await useWebSocketStoreWithOut().closeSocket()
    },

    // 设置 token
    setToken (token = '', refreshToken = '') {
      if (token === '') {
        this.token = '';
        this.refreshToken = '';
      } else {
        this.token = token;
        this.refreshToken = refreshToken;
        // this.loginAfter();
      }
      return this.isLogin;
    },
  },
  persist: {
    enabled: true,
    strategies: [
      {
        key: 'common-store',
        storage: localStorage,
      },
    ],
  },
})
export const useCommonStoreWithOut = () => {
  return useCommonStore(pinia)
}
